$(function(){

	$(".menu span, .div-icon, .a-button").live("mouseover",function(){
			$(this).addClass('ui-state-hover');
	});
	
	$(".menu span, .div-icon, .a-button").live("mouseout",function(){
		$(this).removeClass('ui-state-hover');
	});
    
	$('body *[title].tooltip').tooltip({
		showURL: false,
		showBody: false,
		track: true,
		fade: 30,
		delay: 0,
		extraClass: 'normal'
	});
	$('.notooltip').tooltip().block;
	
	$('html').ajaxComplete(function(){
		$('body *[title].tooltip').tooltip({
			showURL: false,
			showBody: false,
			track: true,
			fade: 30,
			delay: 0
		});
		$('.notooltip').tooltip().block;		
	});
	
	$("#dialog").dialog({
		bgiframe: true,
		resizable: false,
		height:140,
		modal: true,
		draggable: false,
		autoOpen: false,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		}
	});
	
	$(".accordion").accordion({
		autoHeight: false
	});
	
	// Color Picker
	f=$.farbtastic('#colorPickerUIF');
	$('#colorPickerUI').dialog({
		autoOpen: false,
		width: 220,
		height: 210,
		dialogClass: 'colorPickerUI',
		draggable: false,
		resizable: false
	});
	
	$('.colorPicker').click(function(){
		f.linkTo(this);
		f.setColor(rgbtohex($(this).css('background-color')));
		o=$(this).offset();
		o=new Array(o.left, o.top+19);
		$('#colorPickerUI')
			.dialog('option', 'position', o)
			.dialog('open');
	});
	
	$('#colorPickerUIClose').click(function(){
		$('#colorPickerUI').dialog('close');
	});

});

function open_dialog(title, content, boutons){
	$('#dialog').dialog('option', 'title', title);
	$('#dialog').html(content);
	$('#dialog').dialog('option', 'buttons', boutons);
	$('#dialog').dialog('open');
}

function open_loading(){
	open_dialog('Merci de patienter...','<img src="'+REP_INSTALL+'modules/admin/images/loading.gif" />',{});
}

function close_loading(){
	$('#dialog').dialog('close');
}

function set_tooltip(elem){
	$(elem).tooltip({
		showURL: false,
		showBody: false,
		track: true,
		fade: 30,
		delay: 0
	});
}


function rgbtohex(rgbString){
	var parts = rgbString
		.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/)
	;
	// parts now should be ["rgb(0, 70, 255", "0", "70", "255"]

	delete (parts[0]);
	for (var i = 1; i <= 3; ++i) {
	    parts[i] = parseInt(parts[i]).toString(16);
	    if (parts[i].length == 1) parts[i] = '0' + parts[i];
	}
	var hexString = parts.join('');
	return "#"+hexString;
}
